﻿
Imports Guna.UI2.WinForms

Imports System.Data.SqlClient
Imports System.Linq.Expressions

Public Class mainView
    Private Sub 财务支出ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 财务支出ToolStripMenuItem.Click
        财务支出.MdiParent = Me      ' ← 关键：设置父窗体
        财务支出.Show()

        ' 计算子窗体在父窗体中的居中位置
        财务支出.Left = (Me.ClientSize.Width - 财务支出.Width) \ 2
        财务支出.Top = (Me.ClientSize.Height - 财务支出.Height) \ 2
    End Sub

    Private Sub 支付方式ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 支付方式ToolStripMenuItem.Click
        数据字典_支出方式设置.MdiParent = Me
        数据字典_支出方式设置.Show()


        ' 计算子窗体在父窗体中的居中位置
        数据字典_支出方式设置.Left = (Me.ClientSize.Width - 数据字典_支出方式设置.Width) \ 2
        数据字典_支出方式设置.Top = (Me.ClientSize.Height - 数据字典_支出方式设置.Height) \ 2
    End Sub

    Private Sub 测试连接ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 测试连接ToolStripMenuItem.Click
        If DbConnectionHelper.TestConnection() Then
            MessageBox.Show("数据库连接成功！", "测试结果", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            MessageBox.Show("数据库连接失败，请检查配置！", "测试结果", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        End If

    End Sub

    Private Sub 支出查询ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 支出查询ToolStripMenuItem.Click
        支出查询.MdiParent = Me
        支出查询.Show()

        ' 计算子窗体在父窗体中的居中位置
        支出查询.Left = (Me.ClientSize.Width - 支出查询.Width) \ 2
        支出查询.Top = (Me.ClientSize.Height - 支出查询.Height) \ 2
    End Sub

    Private Sub 新建计划ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 新建计划ToolStripMenuItem.Click
        新建计划.MdiParent = Me
        新建计划.Show()

        ' 计算子窗体在父窗体中的居中位置
        新建计划.Left = (Me.ClientSize.Width - 新建计划.Width) \ 2
        新建计划.Top = (Me.ClientSize.Height - 新建计划.Height) \ 2
    End Sub

    Private Sub 计划查询ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 计划查询ToolStripMenuItem.Click
        计划查询.MdiParent = Me
        计划查询.Show()

        ' 计算子窗体在父窗体中的居中位置
        计划查询.Left = (Me.ClientSize.Width - 计划查询.Width) \ 2
        计划查询.Top = (Me.ClientSize.Height - 计划查询.Height) \ 2
    End Sub

    Private Sub main_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        '展示统计
        If getConfig(1) = "是" Then
            loadTjdata()
            统计_GroupBox.Visible = True
        Else
            统计_GroupBox.Visible = False
        End If
    End Sub

    Private Sub loadTjdata()


        ' 2️⃣ 建立连接对象
        Using conn As New SqlConnection(DbConnectionHelper.GetConnectString())
            Try
                ' 打开连接
                conn.Open()

                ' 3️⃣ 创建 SqlCommand 对象，指定要执行的存储过程名称
                Using cmd As New SqlCommand("usp_GetTJdata", conn)
                    cmd.CommandType = CommandType.StoredProcedure  ' 告诉系统这是个存储过程

                    ' 4️⃣ 执行存储过程并获取结果（存储过程里有 SELECT）
                    Using reader As SqlDataReader = cmd.ExecuteReader()

                        ' 5️⃣ 判断是否有返回数据
                        If reader.HasRows Then
                            While reader.Read()
                                ' 读取每一列的值（列名要和存储过程中的 SELECT 输出字段一致）
                                Dim jrRz As Decimal = reader("今日入账")
                                Dim jrZc As Decimal = reader("今日支出")
                                Dim bzRz As Decimal = reader("本周入账")
                                Dim bzZc As Decimal = reader("本周支出")
                                Dim bzJhs As Integer = reader("本周创建计划")
                                Dim bzJjhs As Integer = reader("本周涉及计划")
                                Dim weekStart As DateTime = reader("本周开始")
                                Dim weekEnd As DateTime = reader("本周结束")

                                ' 6️⃣ 在界面上显示（这里举例用 MessageBox）
                                'MessageBox.Show(
                                '    $"📅 今日入账：{jrRz}" & vbCrLf &
                                '    $"💰 今日支出：{jrZc}" & vbCrLf &
                                '    $"📈 本周入账：{bzRz}" & vbCrLf &
                                '    $"📉 本周支出：{bzZc}" & vbCrLf &
                                '    $"🗓 本周创建计划：{bzJhs}" & vbCrLf &
                                '    $"🗂 本周涉及计划：{bzJjhs}" & vbCrLf &
                                '    $"⏰ 本周时间：{weekStart} - {weekEnd}",
                                '    "统计结果", MessageBoxButtons.OK, MessageBoxIcon.Information
                                ')
                                Label1.Text = jrZc
                                Label2.Text = jrRz
                                Label3.Text = bzZc
                                Label4.Text = bzRz
                                Label5.Text = bzJhs
                                Label6.Text = bzJjhs
                                Label7.Text = $"{weekStart} - {weekEnd}"


                            End While
                        Else
                            MessageBox.Show("没有查询到任何结果。", "提示")
                        End If
                    End Using

                End Using

            Catch ex As Exception
                MessageBox.Show("执行出错：" & ex.Message)
            End Try
        End Using

    End Sub
    Private Sub mainView_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        ' 强制关闭所有窗体和线程
        Application.Exit()
        ' 再次确保彻底结束进程
        End
    End Sub
    ' “重置系统”菜单点击事件
    Private Sub 重置系统ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 重置系统ToolStripMenuItem.Click
        ' 弹出确认提示，防止误操作
        Dim result = MessageBox.Show("⚠️ 确定要重置系统吗？此操作将清空相关数据！", "确认操作", MessageBoxButtons.YesNo, MessageBoxIcon.Warning)

        If result = DialogResult.No Then
            Exit Sub
        End If

        Try
            ' 建立数据库连接
            Using conn As New SqlConnection(DbConnectionHelper.GetConnectString)
                conn.Open()

                ' 创建命令对象
                Using cmd As New SqlCommand("usp_Clear", conn)
                    cmd.CommandType = CommandType.StoredProcedure

                    ' 执行存储过程
                    cmd.ExecuteNonQuery()

                    MessageBox.Show("✅ 系统已成功重置！", "操作成功", MessageBoxButtons.OK, MessageBoxIcon.Information)
                End Using
            End Using

        Catch ex As Exception
            ' 捕获异常并提示
            MessageBox.Show("❌ 重置系统失败：" & vbCrLf & ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub 参数管理ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 参数管理ToolStripMenuItem.Click
        参数管理.MdiParent = Me
        参数管理.Show()

        ' 计算子窗体在父窗体中的居中位置
        参数管理.Left = (Me.ClientSize.Width - 参数管理.Width) \ 2
        参数管理.Top = (Me.ClientSize.Height - 参数管理.Height) \ 2
    End Sub

    Private Sub 财务入账ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 财务入账ToolStripMenuItem.Click
        财务入账.MdiParent = Me
        财务入账.Show()

        ' 计算子窗体在父窗体中的居中位置
        财务入账.Left = (Me.ClientSize.Width - 财务入账.Width) \ 2
        财务入账.Top = (Me.ClientSize.Height - 财务入账.Height) \ 2
    End Sub

    Private Sub 收入查询ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 收入查询ToolStripMenuItem.Click
        入账查询.MdiParent = Me
        入账查询.Show()

        ' 计算子窗体在父窗体中的居中位置
        入账查询.Left = (Me.ClientSize.Width - 入账查询.Width) \ 2
        入账查询.Top = (Me.ClientSize.Height - 入账查询.Height) \ 2
    End Sub

    Private Sub 系统重启ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 系统重启ToolStripMenuItem.Click
        ' 提示确认
        Dim result = MessageBox.Show("是否确定要重启系统？", "重启系统", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If result = DialogResult.Yes Then

            ' 获取当前可执行文件路径
            Dim exePath As String = Application.ExecutablePath

            ' 启动新的进程（即自己）
            Process.Start(exePath)

            ' 退出当前程序
            Application.Exit()
        End If
    End Sub

    Private Sub 关于ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 关于ToolStripMenuItem.Click
        关于.MdiParent = Me
        关于.Show()

        ' 计算子窗体在父窗体中的居中位置
        关于.Left = (Me.ClientSize.Width - 关于.Width) \ 2
        关于.Top = (Me.ClientSize.Height - 关于.Height) \ 2
    End Sub

    Private Sub 系统启用ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 系统启用ToolStripMenuItem.Click
        期初设置.MdiParent = Me
        期初设置.Show()

        ' 计算子窗体在父窗体中的居中位置
        期初设置.Left = (Me.ClientSize.Width - 期初设置.Width) \ 2
        期初设置.Top = (Me.ClientSize.Height - 期初设置.Height) \ 2
    End Sub

    Private Sub 财务台账ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 财务台账ToolStripMenuItem.Click
        财务台账.MdiParent = Me
        财务台账.Show()

        ' 计算子窗体在父窗体中的居中位置
        财务台账.Left = (Me.ClientSize.Width - 财务台账.Width) \ 2
        财务台账.Top = (Me.ClientSize.Height - 财务台账.Height) \ 2
    End Sub

    Private Sub 我的金额ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 我的金额ToolStripMenuItem.Click

        ' 2️⃣ 建立连接对象
        Using conn As New SqlConnection(DbConnectionHelper.GetConnectString())
            Try
                ' 打开连接
                conn.Open()

                ' 3️⃣ 创建 SqlCommand 对象，指定要执行的存储过程名称
                Using cmd As New SqlCommand("usp_GetWdye", conn)
                    cmd.CommandType = CommandType.StoredProcedure  ' 告诉系统这是个存储过程

                    ' 4️⃣ 执行存储过程并获取结果（存储过程里有 SELECT）
                    Using reader As SqlDataReader = cmd.ExecuteReader()

                        ' 5️⃣ 判断是否有返回数据
                        If reader.HasRows Then
                            While reader.Read()
                                ' 读取每一列的值（列名要和存储过程中的 SELECT 输出字段一致）
                                Dim wdye As Decimal = reader("金额")
                                MessageBox.Show("余额:" + wdye.ToString, "提示")
                            End While
                        Else
                            MessageBox.Show("系统未设置期初！", "提示")
                        End If
                    End Using

                End Using

            Catch ex As Exception
                LogHelper.WriteLog("获取我的余额错误：" & ex.Message)
                MessageBox.Show("执行出错：" & ex.Message)
            End Try
        End Using
    End Sub

    Private Sub 财务盘点ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 财务盘点ToolStripMenuItem.Click
        财务盘点.MdiParent = Me
        财务盘点.Show()

        ' 计算子窗体在父窗体中的居中位置
        财务盘点.Left = (Me.ClientSize.Width - 财务盘点.Width) \ 2
        财务盘点.Top = (Me.ClientSize.Height - 财务盘点.Height) \ 2
    End Sub
End Class

